package net.xdclass.forum.controller;


import net.xdclass.forum.domain.Reply;
import net.xdclass.forum.domain.Topic;
import net.xdclass.forum.domain.User;
import net.xdclass.forum.dto.PageDTO;
import net.xdclass.forum.service.CategoryService;
import net.xdclass.forum.service.TopicService;
import net.xdclass.forum.service.impl.CategoryServiceImpl;
import net.xdclass.forum.service.impl.TopicServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "TopicServlet", urlPatterns = "/topic")
public class TopicServlet extends BasedServlet{

    private static int pageSize = 2;
    private TopicService topicService = new TopicServiceImpl();
    private CategoryService categoryService = new CategoryServiceImpl();
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int cId = Integer.parseInt(request.getParameter("c_id"));

        //默认第一页
        int page = 1;
        String currentPage = request.getParameter("page");
        if(currentPage != null && currentPage != ""){
            page = Integer.parseInt(currentPage);
        }

        PageDTO<Topic> pageDTO = topicService.listTopicPageByCid(cId, page, pageSize);
        System.out.print(pageDTO.toString());

        request.getSession().setAttribute("categoryList", categoryService.list());
        request.setAttribute("topicPage", pageDTO);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    public  void  findDetailById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //默认第topicId为1
        int topicId = 2;
        String currentTopicId = request.getParameter("topicId");
        if(currentTopicId != null && currentTopicId != ""){
            topicId = Integer.parseInt(currentTopicId);
        }

        //默认第一页
        int page = 1;
        String currentPage = request.getParameter("page");
        if(currentPage != null && currentPage != ""){
            page = Integer.parseInt(currentPage);
        }

        Topic topic = topicService.findById(topicId);
        System.out.print(topic.toString());
        PageDTO<Reply> pageDTO = topicService.findReplyPageByTopicId(topicId, page, pageSize);
        System.out.print(pageDTO.toString());

        request.setAttribute("topic", topic);
        request.setAttribute("replyPage", pageDTO);

        request.getRequestDispatcher("/topic_detail.jsp").forward(request, response);
    }

    /*
    发布主题
     */
    public void addTopic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询是否登录
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        //没登陆直接跳转到登录界面
        if(loginUser == null) {
            request.getRequestDispatcher("/user/login.jsp").forward(request, response);
        }

        String title = request.getParameter("title");
        int cId = Integer.parseInt(request.getParameter("c_id"));
        String content= request.getParameter("content");

        int row = 0;
        row = topicService.addTopic(loginUser,title, cId, content);

        response.sendRedirect("/topic?method=list&c_id="+cId);

    }

    /*
    盖楼回复
     */
    public void replyById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询是否登录
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        //没登陆直接跳转到登录界面
        if(loginUser == null) {
            request.getRequestDispatcher("/user/login.jsp").forward(request, response);
        }
        else {

            int topicId = Integer.parseInt (request.getParameter("topic_id"));
            String content = request.getParameter("content");

            int row = topicService.replyByTopicId(loginUser, topicId, content);
            response.sendRedirect("/topic?method=findDetailById&topicId=" + topicId);
        }




    }


}
